//SPDX-FileCopyrightText: Copyright 2022-2024 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

package issue_dao

import (
	"context"

	"atomgit.com/openlinksaas-org/api-server/dao/db_ns"
	"atomgit.com/openlinksaas-org/proto-gen-go.git/project_sprit_api"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
)

type SummaryItemInfoDbItem struct {
	SummaryItemId string `bson:"_id"`
	SpritId       string `bson:"spritId"`
	TagId         string `bson:"tagId"`
	Content       string `bson:"content"`
	CreateUserId  string `bson:"createUserId"`
	CreateTime    int64  `bson:"createTime"`
	GroupId       string `bson:"groupId"`
}

func (item *SummaryItemInfoDbItem) ToSummaryItem() *project_sprit_api.SummaryItemInfo {
	return &project_sprit_api.SummaryItemInfo{
		SummaryItemId: item.SummaryItemId,
		Content:       item.Content,
		CreateUserId:  item.CreateUserId,
		CreateTime:    item.CreateTime,
		GroupId:       item.GroupId,
		TagInfo: &project_sprit_api.SummaryTag{
			TagId: item.TagId,
		},
	}
}

type _SummaryItemDao struct {
	mc *mongo.Collection
}

func newSummaryItemDao(db *mongo.Database) (*_SummaryItemDao, error) {
	mc := db.Collection(db_ns.DB_NS_ISSUE + "summary_item")
	_, err := mc.Indexes().CreateOne(context.Background(), mongo.IndexModel{
		Keys: bson.D{{"spritId", 1}},
	})
	if err != nil {
		return nil, err
	}
	return &_SummaryItemDao{mc: mc}, nil
}

func (db *_SummaryItemDao) Insert(ctx context.Context, dbItem *SummaryItemInfoDbItem) error {
	_, err := db.mc.InsertOne(ctx, dbItem)
	return err
}

func (db *_SummaryItemDao) Remove(ctx context.Context, spritId, summaryItemId string) error {
	_, err := db.mc.DeleteOne(ctx, bson.M{"_id": summaryItemId, "spritId": spritId})
	return err
}

func (db *_SummaryItemDao) RemoveBySprit(ctx context.Context, spritId string) error {
	_, err := db.mc.DeleteMany(ctx, bson.M{"spritId": spritId})
	return err
}

func (db *_SummaryItemDao) Exist(ctx context.Context, spritId, summaryItemId string) (bool, error) {
	count, err := db.mc.CountDocuments(ctx, bson.M{"_id": summaryItemId, "spritId": spritId})
	return count > 0, err
}

func (db *_SummaryItemDao) Get(ctx context.Context, spritId, summaryItemId string) (*SummaryItemInfoDbItem, error) {
	retItem := &SummaryItemInfoDbItem{}
	err := db.mc.FindOne(ctx, bson.M{"_id": summaryItemId, "spritId": spritId}).Decode(retItem)
	return retItem, err
}

func (db *_SummaryItemDao) UpdateTagAndContent(ctx context.Context, spritId, summaryItemId, tagId, content string) error {
	_, err := db.mc.UpdateOne(ctx, bson.M{"_id": summaryItemId, "spritId": spritId}, bson.M{"$set": bson.M{"tagId": tagId, "content": content}})
	return err
}

func (db *_SummaryItemDao) UpdateGroup(ctx context.Context, summaryItemIdList []string, spritId, groupId string) error {
	_, err := db.mc.UpdateMany(ctx, bson.M{"spritId": spritId, "_id": bson.M{"$in": summaryItemIdList}},
		bson.M{"$set": bson.M{"groupId": groupId}})
	return err
}

func (db *_SummaryItemDao) List(ctx context.Context, spritId string, filterByUser bool, userId string, filterByTagId bool, tagId string) ([]*SummaryItemInfoDbItem, error) {
	query := bson.M{"spritId": spritId}
	if filterByUser {
		query["createUserId"] = userId
	}
	if filterByTagId {
		query["tagId"] = tagId
	}
	cursor, err := db.mc.Find(ctx, query, options.Find().SetSort(bson.M{"createTime": 1}))
	if err != nil {
		return nil, err
	}
	retList := []*SummaryItemInfoDbItem{}
	err = cursor.All(ctx, &retList)
	return retList, err
}

func (db *_SummaryItemDao) CountById(ctx context.Context, summaryItemIdList []string, spritId string) (uint32, error) {
	count, err := db.mc.CountDocuments(ctx, bson.M{"spritId": spritId, "_id": bson.M{"$in": summaryItemIdList}})
	return uint32(count), err
}
